package com.wxn.learncompose.pagers

import AnimState
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Icon
import android.widget.ImageView
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateColor
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.updateTransition
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.github.penfeizhou.animation.loader.ResourceStreamLoader
import com.github.penfeizhou.animation.webp.WebPDrawable
import com.wxn.learncompose.MyApp
import com.wxn.learncompose.R
import kotlinx.coroutines.delay

@Composable
fun AnimPager2() {
    var selectedAnimState : AnimState by remember{ mutableStateOf(AnimState.END) }
    val updateTranse = updateTransition(targetState = selectedAnimState, label = "swith_page_anim")
    val animColor :Color by updateTranse.animateColor(label = "") {
        when(it) {
            AnimState.START -> Color(0xFF000000)
            AnimState.END -> Color.Transparent
            else -> Color.Transparent
        }
    }

    var step :Int by remember { mutableStateOf(0) }

    val animOffetY : Float by updateTranse.animateFloat(label = "" ) {
        when(it) {
            AnimState.START ->  -500f //目标
            AnimState.END -> if (step == 2) -500f else 0f  //开始
            else -> {0f}
        }
    }

    val animScale : Float by updateTranse.animateFloat(label = "") {
        when(it) {
            AnimState.START ->  1.0f
            AnimState.END -> 0f
            else -> {0f}
        }
    }

    LaunchedEffect(step) {
        if (step == 1) {
            delay(1000)
            step = 2
            selectedAnimState = AnimState.END
        }
        if (step == 2) {
            delay(300)
            step = 0
        }
    }

    Box(modifier = Modifier
        .fillMaxSize()
        .clickable {
            if (step == 0) {
                step = 1
                selectedAnimState = AnimState.START
            }
        }){
        Image(painter = painterResource(id = R.drawable.wallpaper),  //背景图片
            contentDescription = null,
            modifier = Modifier.fillMaxSize(),
            contentScale = ContentScale.Crop)

        Spacer(modifier = Modifier  //黑色背景
            .fillMaxSize()
            .background(animColor))

        Icon(
            Icons.Filled.AccountCircle, null,        //前景icon
            modifier = Modifier
                .size(64.dp)
                .align(Alignment.Center)
                .graphicsLayer {
//                    this.translationY = -200.dp.toPx()
                    this.translationY = animOffetY
                    this.scaleX = animScale
                    this.scaleY = animScale
                    this.alpha = animScale
                }
                .clip(CircleShape),
            tint = Color.Green)

    }
}
